<template>
  <div class="menus-body">
    <div class="menus">
      <div
        class="menu-item"
        v-for="(item, index) in cities"
        :key="index"
        @mouseover="showMenu(index)"
        @mouseout="hiddenMenu"
      >
        <span>
          <i v-html="item.icon"></i>
          {{ item.type }}
        </span>
        <div class="menus-hidde" :class="currentOption == index && isshow ? 'active' : ''">
          <ul>
            <li v-for="(data, num) in item.children" :key="num">
              <span class="num">{{ num + 1 }}</span>
              <span class="city" @click="sendCity(data.city)">
                {{
                data.city
                }}
              </span>
              <span class="content">{{ data.desc }}</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cities: [],
      isshow: false,
      currentOption: 0, // 当前选中的选项
    };
  },
  mounted() {
    this.$axios({
      url: "/posts/cities",
    }).then((res) => {
      console.log(res);
      this.cities = res.data.data.map((el, i) => {
        if (i == 0) {
          return {
            ...el,
            icon: '<i class="iconfont" style="color:red">&#xe648;</i>',
          };
        }
        if (i == 1) {
          return {
            ...el,
            icon: '<i class="iconfont" style="color:orange">&#xe60c;</i>',
          };
        }
        if (i == 2) {
          return {
            ...el,
            icon: '<i class="iconfont" style="color:#157efb">&#xe6cc;</i>',
          };
        }
        if (i == 3) {
          return {
            ...el,
            icon: '<i class="iconfont" style="color:#ff8080">&#xe6c2;</i>',
          };
        }
      });
      // console.log("面板数据", this.cities);
    });
  },
  methods: {
    showMenu(index) {
      this.isshow = true;
      this.currentOption = index;
      // console.log(this.currentOption);
    },
    hiddenMenu() {
      this.isshow = false;
    },
    //点击城市名称传递名称给父组件
    sendCity(cityName) {
      this.$emit("acceptCity", cityName);
    },
  },
};
</script>

<style lang="less" scoped>
@import "@/static/css/iconfont.css";
.menus-body {
  position: relative;
  width: 260px;
  .menus {
    position: relative;
    border: 1px solid #ddd;
    border-right: none;
    border-bottom: none;
    box-shadow: 0 0 1px #f5f5f5;
    z-index: 2;
    .menu-item {
      cursor: pointer;
      font-size: 14px;
      height: 40px;
      line-height: 36px;
      padding: 0 20px;
      border-bottom: 1px solid #ddd;
      border-right: 1px solid #ddd;
      &:after {
        float: right;
        font-family: "iconfont";
        content: "\e631";
      }
      &:hover {
        border-right: 2px solid #fff;
        color: orange;
      }
    }
  }

  .menus-hidde {
    z-index: 2;
    position: absolute;
    box-sizing: border-box;
    top: -1px;
    left: 259px;
    width: 340px;
    background-color: #fff;
    border-top: 1px solid #ccc;
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    display: none;
    ul {
      margin-left: -1px;
      li {
        padding: 0 20px;
        font-size: 14px;
        &:last-child {
          border-left: 1px solid #ccc;
        }
        .num {
          font-size: 26px;
          color: orange;
          font-style: italic;
        }
        .city {
          color: orange;
          margin: 0 10px;
          cursor: pointer;
          &:hover {
            text-decoration: underline;
          }
        }
        .content {
          color: #9d9d9e;
          cursor: default;
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
  .active {
    display: block;
  }
}
</style>
